package com.juyu.miliao.api.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.api.dao.mysql.SystemMessageDao;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统消息im工具类
 * 
 * @author wbf
 */
@Component
public class SystemMessageUtil {

	private final Logger logger = LoggerFactory.getLogger(SystemMessageUtil.class);
	@Resource
	SystemMessageDao systemMessageDao;
	/**
	 * 新增用户送30金币
	 * @param userId
	 */
	public void newUser(Long userId) {
		try {
			String message = systemMessageDao.getMessageByKey("newUser");
	        Map<String,String> map=new HashMap<String,String>();  
	        map.put("title", "寻寻");
	        map.put("type", "1");
	        map.put("message", message);
	        ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("newUser系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
				logger.error("newUser系统消息发送IM错误：参数-" + userId,e);

		}
	}
	/**
	 * 邀请用户注册20金币
	 * @param userId
	 */
	public void shareUser(Long userId) {
		try {
			String message = systemMessageDao.getMessageByKey("shareUser");
	        Map<String,String> map=new HashMap<String,String>();  
	        map.put("title", "寻寻");
	        map.put("type", "1");
	        map.put("message", message);
	        ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("shareUser系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}
			}
		} catch (Exception e) {
			logger.error("shareUser系统消息发送IM错误：参数-" + userId,e);
		}
	}
	/**
	 * 金币充值成功提示
	 * @param userId
	 */
	public void userRecharge(long userId,String coinNumber,double price) {
		try {
			//"恭喜您成功充值"+price+"元"+"获得"+coinNumber+"！"
			String message = SpendSystemMessage.RECHARGE_DEPOSIT_SEND_MESSAGE.getMessage(price,coinNumber);
			logger.info("发送寻币充值通知：userId={},message={}", userId, message);
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", "寻寻");
	        map.put("type", "1");
	        map.put("message", message);
	        ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("userRecharge系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
				logger.error("userRecharge系统消息发送IM错误：参数-" + userId,e);
		}
	}

	/**
	 * vip充值成功提示（点对点系统消息发）
	 * @param userId
	 */
	public void userVip(long userId) {
		try {
			String message = SpendSystemMessage.RECHARGE_VIP_SEND_MESSAGE.getMessage();
			logger.info("发送VIP充值通知：userId={},message={}", userId, message);
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", "寻寻");
			map.put("type", "1");
			map.put("message", message);
			ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("充值vip系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
			logger.error("充值vip系统消息发送IM错误：参数-" + userId,e);
		}
	}

	/**
	 * 充值Svip 发送系统消息
	 * @param userId
	 */
	public void userSVip(long userId) {
		String message = SpendSystemMessage.RECHARGE_SVIP_SEND_MESSAGE.getMessage();
		logger.info("发送SVIP充值通知：userId={},message={}", userId, message);
		sendSystemMessage(userId,message);
	}

	/**
	 * 充值铂金卡发送系统消息
	 * @param userId
	 */
	public void userPlatinumVip(long userId) {
		String message = SpendSystemMessage.RECHARGE_PLATINUM_VIP_SEND_MESSAGE.getMessage();
		logger.info("发送铂金卡充值通知：userId={},message={}", userId, message);
		sendSystemMessage(userId,message);
	}

	public void sendSystemMessage(long userId,String message){
		try {
			logger.info("发送系统消息通知：userId={},message={}", userId, message);
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", "寻寻");
			map.put("type", "1");
			map.put("message", message);
			ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("充值vip系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
			logger.error("充值vip系统消息发送IM错误：参数-" + userId,e);
		}
	}

	/**
	 * 用户11级发送系统消息
	 */
	public void sendSystemMessageByUser11(long userId,String message){
		try {
			logger.info("发送系统消息通知：userId={},message={}", userId, message);
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", "寻寻");
			map.put("type", "1");
			map.put("message", message);
			ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("发送用户到11级系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
			logger.error("发送用户到11级系统消息发送IM错误：参数-" + userId,e);
		}
	}
	/**
	 * 举报成功 客服小助手提示
	 * (举报用户专用客服消息)
	 * @param userId
	 */
	public void userReport(long userId,String niceName) {
		String message = "举报"+niceName+"成功，我们将尽快核实，感谢您对平台的支持";
		logger.info("举报成功充值通知：userId={},message={}", userId, message);
		sendSystemMessage(userId,message);
	}
	/**
	 * 意见反馈成功 客服小助手提示
	 * @param userId
	 */
	public void userFeedBack(long userId) {
		String message = "您好，我们已经收到您的反馈，我们将尽快核实后与您沟通，如有问题请在“我的-我的客服”咨询~";
		logger.info("意见反馈消息通知：userId={},message={}", userId, message);

		JSONObject body = new JSONObject();
		body.put("msg", message);
		//开发者扩展字段
		JSONObject ext = new JSONObject();

		try {
			String temp = NetEaseCloudUserService.getSendMsg(userId, EnumType.HELPER_USER_ID.state,
					EnumType.TEXT_MESSAGE.state, body.toString(), ext.toString(), "");
			logger.info("举报成功发送普通消息:user={},body={}",userId,body.toString());
			if (!temp.substring(temp.indexOf("code") + 6, temp.indexOf("code") + 9).equals("200")) {
				logger.error("举报成功发送普通消息异常-云信：userId={},message={}", userId, temp);
			}

		} catch (Exception e) {
			logger.error("举报成功发送普通消息异常：userId={},error={}", userId, e);
		}
	}

	/**
	 * 提现成功提示
	 * @param userId
	 */
	public void userCash(Long userId) {
		try {
			String message = systemMessageDao.getMessageByKey("userCash");
	        Map<String,String> map=new HashMap<String,String>();  
	        map.put("title", "寻寻");
	        map.put("type", "1");
	        map.put("message", message);
	        ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("userCash系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}
			}
		} catch (Exception e) {
			logger.error("userCash系统消息发送IM错误：参数-" + userId,e);

		}
	}

	/**
	 * 发送大客户消息
	 * @param userId
	 */
	public void spendBigCustomersMessage(long userId,String message){
		try {
			logger.info("发送大客户消息通知：userId={},message={}", userId, message);
			Map<String, String> map = new HashMap<String, String>(3);
			map.put("title", "寻寻");
			map.put("type", "1");
			map.put("message", message);
			ObjectMapper mapper = new ObjectMapper();
			String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
			if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
				if(logger.isErrorEnabled()){
					logger.error("发送大客户消息IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
				}

			}
		} catch (Exception e) {
			logger.error("发送大客户消息IM错误：参数-" + userId,e);
		}
	}
}
